package com.example.testapp.activity.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.example.testapp.activity.http.RetryNetwork;
import com.example.testapp.activity.listener.PwdErrorListener;
import com.example.testapp.activity.ui.Task;
import com.example.testapp.activity.util.BitmapUtils;
import com.example.testapp.activity.util.NetWorkCenter;
import com.example.testapp.activity.util.ShareUtils;
import com.example.testapp.activity.util.ThreadPoolManager;
import com.example.testapp.activity.util.UserController;

/**
 * Created by l on 2016/8/23.
 */
public abstract class SuperActivity extends FragmentActivity implements RetryNetwork, PwdErrorListener {
    private static final String TAG = "BaseActivity";

    protected boolean isAvtive = false;
    protected boolean isPwdError = false;
    protected Context mContext;
    protected FragmentManager  fmm;
    protected ThreadPoolManager mThreadPoolManager;
    protected ProgressDialog progress;
    protected NetWorkCenter mNetWorkCenter;

    protected ShareUtils mShareUtils;
    protected UserController  mUserController;

    protected BitmapUtils mBitmapUtils;

    /**
     * 用于判断APP是否是正常启动,还是奔溃后自动重启的
     */
    protected static boolean isNormalStart = false;

    /**
     * 当前最后执行的线程任务,task的ID属性可以用于判断线程启动的先后
     */
    protected Task task = new Task(0){
        @Override
        public void run() {

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        isAvtive = true;
        mShareUtils = new ShareUtils(this);
        fmm = getSupportFragmentManager();
        mUserController = UserController.getInstance(mContext.getApplicationContext());
        mThreadPoolManager = ThreadPoolManager.getInstance();
        mBitmapUtils = new BitmapUtils(mContext);
    }

    public void showProgressDialog() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (progress == null){
                    progress = new ProgressDialog(mContext);
                    progress.setMessage("正在加载,请稍后...");
                    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                    progress.setCancelable(true);
                }
                progress.show();
            }
        });
    }

    @Override
    public void pwdError() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                isPwdError = true;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        obtainInfo();
        isAvtive = true;

        if(!isNormalStart){

        }

        if(mNetWorkCenter != null){
            mNetWorkCenter.setRetryNetwork(this);
        }
    }

    /**
     * 接收数据
     */
    protected abstract void obtainInfo();

    @Override
    protected void onPause() {
        super.onPause();
        isAvtive = false;

        if(mNetWorkCenter != null){
            mNetWorkCenter.removeRetry();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isAvtive = false;

        if(mNetWorkCenter != null){
            mNetWorkCenter.removeRetry();
        }
    }

    public boolean getAvtive() {
        return isAvtive;
    }

    /**
     * 隐藏输入法
     *
     * @param context
     * @param achor
     */
    public static void hideSoftInput(Context context, View achor) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(achor.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 弹出吐司
     * @param msg
     */
    protected void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void retry() {

    }

    @Override
    public void netError() {

    }
}
